package cn.suimg.shortlink.controller;

import cn.suimg.shortlink.request.BasicController;
import cn.suimg.shortlink.service.ShortLinkService;
import cn.suimg.web.util.PrincipalUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.security.Principal;

@Controller
@RequestMapping("/")
public class IndexController extends BasicController {

    @Resource
    private ShortLinkService shortLinkService;


    @RequestMapping("index.do")
    public String index(Principal principal){
        if(principal != null){
            String currentToken = PrincipalUtil.getCurrentToken(principal);
            setAttribute("token",currentToken);
        }
        return "index";

    }

    @RequestMapping("{shortLinkId}")
    public String shortLink(@PathVariable String shortLinkId){
        //TODO 记录调用信息
//        String referer = getReferer();
//        String ipAddress = getIPAddress();
//        String userAgent = getUserAgent();
//        String userId = getJSessionId();
        String originalLink = shortLinkService.getOriginalLink(shortLinkId);
        setAttribute("originalLink",originalLink);
        return "redirect";
    }
}
